package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Good;
import com.yc.bean.Types;
import com.yc.bean.User;
import com.yc.biz.GoodBiz;
import com.yc.biz.TypesBiz;
import com.yc.biz.UserBiz;
import com.yc.biz.impl.GoodBizImpl;
import com.yc.biz.impl.TypesBizImpl;
import com.yc.biz.impl.UserBizImp1;

/**
 * @author Huathy
 * 这个类包含所有与管理员端相关的请求操作
 */
@WebServlet("/admin_type")
public class Admin_TypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	TypesBiz tb = new TypesBizImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		PrintWriter out = resp.getWriter();
		if( "getTypeByPage".equals(op) ){
			getTypeByPage(req,resp,out);
		}else if ("type_modify".equals(op)) {
			type_modify(req, resp, out);
		}else if ("type_del".equals(op)) {
			type_del(req, resp, out);
		}else if ("type_add".equals(op)) {
			type_add(req, resp, out);
		}else if ("getAllTypes".equals(op)) {
			getAllTypes(req, resp, out);
		}
	}
	
	/**
	 * 获得所有类型
	 * @param req
	 * @param resp
	 * @param out
	 * 
	 */
	protected void getAllTypes(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		List<Types> types = tb.getAllTypes();
		req.getSession().setAttribute("types", types);
		out.print(types.size());
	}

	//类型添加
	private void type_add(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		String tname = req.getParameter("tname");
		if(tname==null || tname==""){
			out.print(-1);
			return;
		}
		int result = tb.type_add(tname);
		out.print(result);
	}

	/**
	 * 管理员删除类型
	 * @param req
	 * @param resp
	 * @param out
	 */
	private void type_del(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int tid = -1;
		try {
			tid = Integer.parseInt( req.getParameter("tid") );
		} catch (NumberFormatException e) {
			e.printStackTrace();
			out.print(-1);
		}
		int result = tb.type_del(tid);
		out.print(result);
	}

	//类型修改
	private void type_modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int tid = -1;
		String tname = null;
		try {
			tid = Integer.parseInt( req.getParameter("tid") );
			tname = req.getParameter("tname");
		} catch (Exception e) {
//			e.printStackTrace();
			out.print(-1);
			return;
		}
		
		int result = tb.type_modify(tid,tname);
		out.print(result);
	}


	
	//获取类型
	private void getTypeByPage(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int totalPage = 1;
		try {
			totalPage = Integer.parseInt( req.getParameter("typeTotalPage") );
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			totalPage = tb.getTotalPage();
			req.getSession().setAttribute("typeTotalPage", totalPage);
		}
		int page = 1;
		try {
			page = Integer.parseInt( req.getParameter("page") );
		} catch (NumberFormatException e) {
//			e.printStackTrace();
		}
		if(page < 1){
			page = 1;
		}
		if( page >totalPage ){
			page = totalPage;
		}
		List<Types> types = tb.getTypes(page);
		req.getSession().setAttribute("types", types);
		if(types.isEmpty()){
			out.print(-1);
			return;
		}
		out.print(page);
	}


}
